<?php
namespace app\admin\model;

use think\Model;

class Redenvelopes extends Model
{
	//设置主键
    protected $pk = 'id';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'super_redenvelopes';
	protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();;
        //TODO:自定义的初始化
    }
	// 实例化模型
	public function getRedenvelopesPage($where,$order,$arr,$pagesize=12){
		$db = new Redenvelopes();
		$vn = $db->where($where)->order($order)->paginate($pagesize,false,['query'=>$arr]);
		// foreach($vn as $v=>$n){
		// 	if($n['Fate_amount']){
		// 		$n['Fate_amount'] = json_decode($n['Fate_amount'],true);
		// 	}
		// }
        return $vn;
	}
	// 删除记录
	public function deleteRedenvelopes($id){
		$db = new Redenvelopes();
		$where['id'] = ['in',$id];
		if($db->where($where)->delete())
		return json(['code'=>1,'msg'=>'删除成功']);
		return json(['code'=>0,'msg'=>'删除失败']);
	    
	}
	// 更改记录状态
	public function toexamineRedenvelopes($id,$sta){
		$db = new Redenvelopes();
		$where['id'] = ['in',$id];
		if($db->where($where)->setField('State',$sta))
		return json(['code'=>1,'msg'=>'更新成功']);
		return json(['code'=>0,'msg'=>'更新失败']);
	    
	}
	// 查询红包ID
	public function queryRedenvelopesIds($nme,$val='Redenvelopes_id')
	{
		$ids = model('User')->queryUserIds($nme,'User_id');
		$db = new Redenvelopes();
		$where['User_id'] = ['in',$ids];;
		$arr = $db->where($where)->field($val)->select();
		$redids = array_column($arr,$val);
		return implode(',',$redids);
	}
	
}